<template>
  <div>
    <div>
      <jxf-titletip :title="title" @refreshPage="refreshFuc" :hasGoBack="isHasGoBack" @goBack="goPrewview"></jxf-titletip>
      <el-card class="box-card mt20 rechargeHeader" v-if="!isShowPersonModel">
        <span>信用总额: <span class="red">￥{{applicationInfo.credit ? formatMoney.twoDecimal(applicationInfo.credit) : '0.00'}}</span></span>&nbsp;
        <span>可用余额: <span class="red">￥{{applicationInfo.surplusCredit ? formatMoney.twoDecimal(applicationInfo.surplusCredit) : '0.00'}}</span></span>
      </el-card>
      <el-card class="mt20">
        <el-form :inline="true" :model="balanceInfo" ref="balanceInfo">
          <el-form-item label="摘要:" prop="remark">
            <el-select v-model="balanceInfo.remark" clearable placeholder="全部">
              <el-option label="额度发放" value="额度发放"></el-option>
              <el-option label="发放清算" value="发放清算"></el-option>
              <el-option label="支付" value="支付"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="结算事项:" prop="type">
            <el-select v-model="balanceInfo.type" clearable placeholder="全部">
              <el-option label="额度发放" value="0"></el-option>
              <el-option label="发放清算" value="1"></el-option>
              <el-option label="出账" value="2"></el-option>
            </el-select>
          </el-form-item>
          <jxf-Date label="时间段" :startTime.sync="balanceInfo.startTime" :endTime.sync="balanceInfo.endTime"></jxf-Date>
          <el-form-item>
            <el-button type="primary" @click="onSearch()">搜索</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="info" @click="resetForm('balanceInfo')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
      <el-card class="jxf-table-card mt20">
        <div slot="header" class="clearfix">
          <span>数据列表</span>
          <div>
            <el-button type="primary" @click="exportExcel" :loading="isSuccess">导出</el-button>
          </div>
        </div>
        <jxf-table class="" :table="balanceTable">
          <template slot="createTime" slot-scope="account">
            <div>{{account.row.createTime ? FormatDate.timeFormat(account.row.createTime) : ""}}</div>
          </template>
          <template slot="type" slot-scope="account">
            <div v-if="account.row.type === 0">额度发放</div>
            <div v-if="account.row.type === 1">发放清算</div>
            <div v-if="account.row.type === 2">出账</div>
            <div v-else></div>
          </template>
          <template slot="remark" slot-scope="account">
            <div v-if="account.row.remark === 21">鲜丰惠支付</div>
            <div v-if="account.row.remark === 22">充值返现</div>
            <div v-if="account.row.remark === 23">鲜丰惠提现</div>
            <div v-else></div>
          </template>
          <template slot="balance" slot-scope="account">
            <div>￥{{account.row.balance && account.row.balance != 0 ? formatMoney.twoDecimal(account.row.balance) : '0.00'}}</div>
          </template>
          <template slot="score" slot-scope="account">
            <div>￥{{account.row.score && account.row.score != 0 ? formatMoney.twoDecimal(account.row.score) : '0.00'}}</div>
          </template>
        </jxf-table>
      </el-card>
      <jxf-pagination class="mt20" :pageTotal="pageTotal" :currentPage.sync='currentPage' :pageSize.sync="pageSize"
                      @currentPageChange="currentPageChange" @pageSizeChange="pageSizeChange"></jxf-pagination>
    </div>
  </div>
</template>

<script>
  import formatMoney from '@/assets/js/formatMoney'
  import FormatDate from '@/assets/js/formatDate'
  export default {
    name: "XinBalance",
    props: {

    },
    data() {
      return {
        isSuccess: false,
        isHasGoBack: this.$route.params && this.$route.params.userId ? false : false,
        title: this.$route.params && this.$route.params.title ? this.$route.params.title : "鲜丰信用流水",
        url: this.$route.params && this.$route.params.url ? this.$route.params.url : apis.finance.getCreditRecordList,
        formatMoney: formatMoney,
        FormatDate: FormatDate,
        balanceInfo: {
          remark: '',
          endTime: "",
          startTime: "",
          type: ""
        },
        applicationInfo: {
          credit: 0,
          surplusCredit: 0
        },
        balanceTable: {
          border:true,
          loading:false,
          hasSelect:false,
          hasOperation:false,
          tr: [
            {
              id: '1',
              label: '流水编号',
              width: '200',
              prop: 'creditTradeNo'
            },
            {
              id: '2',
              label: '结算事项',
              prop: 'type',
              show: 'template'
            },
            {
              id: '3',
              label: '摘要',
              prop: 'remark',
              // show: 'template'
            },
            {
              id: '4',
              minWidth: '180',
              label: '变动金额金额（元）',
              prop: 'score',
              show: 'template'
            },
            {
              id: '5',
              minWidth: '120',
              label: '余额（元）',
              prop: 'balance',
              show: 'template'
            },
            {
              id: '6',
              width: '190',
              label: '关联订单号',
              prop: 'orderNos'
            },
            {
              id: '7',
              label: '时间',
              width: '160',
              show: 'template',
              prop: 'createTime'
            }
          ],
          operation: [],
          data: []
        },
        pageTotal:40,
        pageSize:10,
        currentPage:1
      }
    },
    methods: {
      // 返回前面一个页面
      goPrewview() {
        window.history.back(-1);
      },
      exportExcel(){
        this.isSuccess = true;
        let data = {
          ...this.balanceInfo,
          userId: this.$route.params ? this.$route.params.userId : ""
        };
        ajax.export(apis.finance.exportXinBalance, data).then((res) => {
          this.isSuccess = false;
        }).catch((err) => {
          this.$message.error(err.message)
        })
      },
      refreshFuc() {
        this.resetForm('balanceInfo')
        this.currentPage = 1;
        this.pageSize = 10;
        this.getData()
      },
      getData() {
        this.balanceTable.loading = true;
        let data = {
          ...this.balanceInfo,
          userId: this.$route.params ? this.$route.params.userId : "",
          pageNum: this.currentPage,
          pageSize: this.pageSize
        };
        ajax.post(this.url, data).then(res => {
          if(res.code === "10000000") {
            this.applicationInfo.credit = res.data.credit
            this.applicationInfo.surplusCredit = res.data.surplusCredit
            this.balanceTable.data = res.data.creditTradeDetailsVoList.dataList;
            this.pageTotal = res.data.creditTradeDetailsVoList.total
          } else {
            this.$message.error(res.message)
          }
          this.balanceTable.loading = false;
        })
      },
      onSearch() {
        this.currentPage = 1;
        this.getData()
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      },
      // 切换页面
      currentPageChange(pageNo) {
        this.currentPage = pageNo;
        this.getData()
      },
      // 切换页面条数
      pageSizeChange(pageNo) {
        this.pageSize = pageNo
        this.getData()
      },
    },
    created() {
      this.onSearch()
    }
  }
</script>

<style scoped>

</style>
